import { AstContext } from "../../../context";

export function createToIndex(ast: AutoJs.UiObject, ctx: AstContext, key: string) {
    return (index: number) => {
        const value = ctx.get(key)
        if (value === index) return true;
        const node = ast.child(index)
        if (node) {
            if (node.clickable()) {
                node.click()
                node.recycle()
                return true;
            } else {
                const clickable = ctx.dfsOne(node, it => it.clickable())
                if (clickable) {
                    clickable.click()
                    clickable.recycle()
                    return true;
                }
            }
        }
        return false;
    }
}
